from django.utils.safestring import mark_safe

from app01.models import UserFields, Users, 预算实绩
from app01.utils.modelform import UserFieldsForm


def fields_to_html(userid, model_name):
    dicts = {}
    # 获得当前用户的记录
    user_row = Users.objects.filter(employeeid=userid)
    # 获得model_name对应的表名
    table_name = model_name._meta.model_name
    table_fields = model_name.objects.filter()[:1]
    # 检查UserFields表中有没有对应记录
    json_row = UserFields.objects.filter(user=user_row.values('id').first()['id'], table_name=table_name).first()
    if not json_row:
        # 没有记录，就生成一个默认值
        fields = list(table_fields.values()[0])
        # print(fields)
        json_row = UserFields()
        json_row.user = user_row.first()
        json_row.table_name = table_name
        for key in fields:
            dicts[key] = [True, model_name._meta.get_field(key).verbose_name]
            # {'id': [True, 'ID'], 'employeeid': [True, '工号'], 'name': [True, '姓名'], 'password': [True, '密码'],
            # 'admin': [True, '管理员'], 'create_time': [True, '创建时间'], 'update_time': [True, '更新时间']}
        # print(dicts)

        json_row.fields_opt = dicts
        json_row.save()
    else:
        dicts = json_row.fields_opt

    select_str_list = ['<select id="col_select" style="width:150px" multiple="multiple" placeholder="选择需显示的列">']
    for k, v in dicts.items():
        # print('%s %s %s' % (k, v[0], v[1]))
        if v[0]:
            select_str_list.append('<option value="{}" selected="selected">{}</option>'.format(k, v[1]))
        else:
            select_str_list.append('<option value="{}">{}</option>'.format(k, v[1]))
    select_str_list.append('</select>')

    select_str =''.join(select_str_list)
    # print(dicts)

    return mark_safe(select_str), dicts


def fields_to_save(employeeid, model_name, fields_list):
    dicts = {}
    # 获得当前用户的记录
    user_row = Users.objects.filter(employeeid=employeeid)
    # 获得model_name对应的表名
    table_name = model_name._meta.model_name
    table_fields = model_name.objects.filter()[:1]
    # 检查UserFields表中有没有对应记录
    json_row = UserFields.objects.filter(user=user_row.values('id').first()['id'], table_name=table_name).first()
    if not json_row:
        json_row = UserFields() # 没有记录，就生成一个
        json_row.user = user_row.first()
        json_row.table_name = table_name

    fields = list(table_fields.values()[0])
    # print(fields)
    for key in fields:
        if key in fields_list:
            dicts[key] = [True, model_name._meta.get_field(key).verbose_name]
        else:
            dicts[key] = [False, model_name._meta.get_field(key).verbose_name]

    json_row.fields_opt = dicts
    json_row.save()
    return True

def yearmonth_to_html(userid, model_name):
    # 获得当前用户的记录
    list_row = 预算实绩.objects.values('年月').distinct().order_by('-年月')
    # print(list_row)
    select_str_list = ['<select id="col_select" style="width:150px" multiple="multiple" placeholder="选择需显示的列">']
    for v in list_row:
        # print('%s %s %s' % (k, v[0], v[1]))
        # if v[0]:
        #     select_str_list.append('<option value="{}" selected="selected">{}</option>'.format(v['年月']))
        # else:
        select_str_list.append('<option value="{}">{}</option>'.format(v['年月'], v['年月']))
        # print(v)
    select_str_list.append('</select>')

    select_str =''.join(select_str_list)
    # print(select_str)

    return mark_safe(select_str)